package com.abdu.demo.app_ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.abdu.demo.app_ui.fragment.FunFragment;
import com.abdu.demo.app_ui.fragment.HomeFragment;
import com.abdu.demo.app_ui.fragment.MyPagerAdapter;
import com.abdu.demo.app_ui.fragment.UserFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tMenuHome, tMenuFun, tMenuUser;
    private View viewT;
    private int screenWidth;

    private ViewPager viewPager;
    private MyPagerAdapter pagerAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_AppUi);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setTitle("首页");

        initView();
        initObject();

        pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                menuInit();
                // 选中新页面时的代码
                if (position==0){
                    menuState(true, tMenuHome, R.drawable.img_home_on);
                    setTitle("首页");
                } else if (position==1) {
                    menuState(true, tMenuFun, R.drawable.img_function_on);
                    setTitle("功能");
                } else {
                    menuState(true, tMenuUser, R.drawable.img_user_on);
                    setTitle("我的");
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                // 页面滚动状态改变时的代码
            }
        });

    }



    private void initView() {
        tMenuHome = findViewById(R.id.t_menu_home);
        tMenuHome.setOnClickListener(this);
        tMenuFun = findViewById(R.id.t_menu_fun);
        tMenuFun.setOnClickListener(this);
        tMenuUser = findViewById(R.id.t_menu_user);
        tMenuUser.setOnClickListener(this);

        viewPager = findViewById(R.id.view_pager);
        viewT = findViewById(R.id.viewT);


        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        screenWidth = displayMetrics.widthPixels;

        viewT.setTranslationX(screenWidth*1.0f/6-viewT.getLayoutParams().width*1.0f/2);



        menuInit();

        menuState(true, tMenuHome, R.drawable.img_home_on);
    }


    private void initObject() {

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {

        menuInit();  //先初始化底部导航栏默认颜色和图标。点击时修改颜色和图片

        if (view.getId()==R.id.t_menu_home){
            menuState(true, tMenuHome, R.drawable.img_home_on);
            setTitle("首页");
            viewPager.setCurrentItem(0, false);


        }

        if (view.getId()==R.id.t_menu_fun){
            menuState(true, tMenuFun, R.drawable.img_function_on);
            setTitle("功能");

            viewPager.setCurrentItem(1, false);
        }


        if (view.getId()==R.id.t_menu_user){
            menuState(true, tMenuUser, R.drawable.img_user_on);
            setTitle("我的");
            viewPager.setCurrentItem(2,false);
        }


    }


    //先初始化底部导航栏默认颜色和图标，点击时修改颜色和图片
    @SuppressLint("ResourceAsColor")
    public void menuInit() {
        menuState(false, tMenuHome, R.drawable.img_home_off);
        menuState(false, tMenuFun, R.drawable.img_function_off);
        menuState(false, tMenuUser, R.drawable.img_user_off);
    }




    @SuppressLint("ResourceAsColor")
    private void menuState(boolean state, TextView textView, int imgId){
        Drawable drawable = ContextCompat.getDrawable(this, imgId);
        drawable.setBounds(0, 0, 56, 56);
        textView.setCompoundDrawables(null, drawable, null, null);
        if (state){
            textView.setTextColor(0xFFFF6000);
            textView.setScaleX(1.15f);
            textView.setScaleY(1.05f);

             if (textView.getId()==R.id.t_menu_home){
                 ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(viewT, "translationX", viewT.getX(), (screenWidth*1.0f/6)-viewT.getLayoutParams().width*1.0f/2);
                 objectAnimator.setDuration(400);
                 objectAnimator.setRepeatCount(0);
                 objectAnimator.start();
             } else if (textView.getId()==R.id.t_menu_fun) {
                 ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(viewT, "translationX", viewT.getX(), (screenWidth*1.0f/6)*3-viewT.getLayoutParams().width*1.0f/2);
                 objectAnimator.setDuration(400);
                 objectAnimator.setRepeatCount(0);
                 objectAnimator.start();
             } else if (textView.getId()==R.id.t_menu_user) {
                 ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(viewT, "translationX", viewT.getX(), (screenWidth*1.0f/6)*5-viewT.getLayoutParams().width*1.0f/2);
                 objectAnimator.setDuration(400);
                 objectAnimator.setRepeatCount(0);
                 objectAnimator.start();
             }

        }
        else{
            textView.setTextColor(0xFF8a8a8a);
            textView.setScaleX(1.0f);
            textView.setScaleY(1.0f);
        }
    }
}